package com.xywy.find;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.viewpagerindicator.TabPageIndicator;
import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.constants.ConstantValue;
import com.xywy.common.util.MLog;
import com.xywy.common.widget.TitleBar2;
import com.xywy.find.bean.Jkgc;
import com.xywy.find.bean.JkgcData;

/**
 * 健康广场 - 专栏 plaze = 0 plaze：专栏id（母婴=1 减肥=2 保健=3 饮食=4）
 * 
 * @author xiaogd
 * 
 */
public class JkgcPlazeActivity extends BaseActivity {

	/*
	 * private static final String[] TITLE = new String[] { "头条", "房产", "另一面",
	 * "女人", "财经", "数码", "情感", "科技","搞笑","娱乐","电影","更多" };
	 */
	private String[] TITLE = new String[] { "母婴", "美容", "保健", "饮食", "减肥" };
	private static final String TAG = "JkgcPlazeActivity";
	private Context mContext;
	private int target = 0;
	private ViewPager pager;
	private List<Jkgc> jkgcList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.preInit();
		this.loadData();
		// 设置为全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.find_jkgc_plaze);

		this.initView();

		this.tabPage();
	}

	@Override
	public void initView() {
		// title
		TitleBar2 titleBar = (TitleBar2) findViewById(R.id.find_jkgc_plaze_title_bar);
		TextView tvTitle = titleBar.getTitle();
		TextView tvBack = titleBar.getTvBack();
		titleBar.getGo().setVisibility(View.INVISIBLE);
		titleBar.onClickBack(this);
		String titlebar = "健康广场";
		if (target != 0) {
			switch (target) {
			case ConstantValue.my:
				titlebar = "母婴";
				break;
			case ConstantValue.jf:
				titlebar = "减肥";
				break;
			case ConstantValue.bj:
				titlebar = "保健";
				break;
			case ConstantValue.ys:
				titlebar = "饮食";
				break;
			}
		}
		tvTitle.setText(titlebar);
		tvBack.setText(getString(R.string.main_tab_tv_find));
	}

	@Override
	public void preInit() {
		mContext = this.context;
		target = getIntent().getIntExtra("target", 0);
		Bundle bundle = getIntent().getBundleExtra("title");
		TITLE = bundle.getStringArray("titles");

		jkgcList = (List<Jkgc>) bundle.getSerializable("jkgcList");

	}

	@Override
	public void loadData() {

	}

	private void tabPage() {
		// ViewPager的adapter
		pager = (ViewPager) findViewById(R.id.pager);

		// 显示tab标题
		FragmentPagerAdapter adapter = new TabPageIndicatorAdapter(
				getSupportFragmentManager());
		pager.setAdapter(adapter);

		// 实例化TabPageIndicator然后设置ViewPager与之关联
		TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
		indicator.setViewPager(pager);

		// 如果我们要对ViewPager设置监听，用indicator设置就行了
		indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				// Toast.makeText(getApplicationContext(), TITLE[arg0],
				// Toast.LENGTH_SHORT).show();
				Stattistics(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

	}

	/**
	 * ViewPager适配器
	 * 
	 * @author len
	 * 
	 */
	class TabPageIndicatorAdapter extends FragmentPagerAdapter {
		public TabPageIndicatorAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			// 新建一个Fragment来展示ViewPager item的内容，并传递参数
			Fragment fragment = new JkgcPlazeFragment();
			Bundle args = new Bundle();
			// args.putString("arg", TITLE[position]);
			List<JkgcData> data = getJkgcData(TITLE[position]);
			if (data != null) {
				args.putInt("target", target);
				args.putString("title", TITLE[position]);
				args.putSerializable("titles", (java.io.Serializable) data);
				fragment.setArguments(args);
			}

			return fragment;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return TITLE[position % TITLE.length];
		}

		@Override
		public int getCount() {
			return TITLE.length;
		}
	}

	private List<JkgcData> getJkgcData(String title) {
		List<JkgcData> datas = null;
		if (null != jkgcList && !jkgcList.isEmpty()) {

			for (int i = 0; i < jkgcList.size(); i++) {
				Jkgc jkgc = jkgcList.get(i);

				if (jkgc.getTypename().equals(title)) {
					MLog.i(TAG, "标题:" + jkgc.getTypename());
					datas = jkgc.getData();
					/*
					 * for( int j=0;j<datas.size();j++){ JkgcData
					 * data=datas.get(j); MLog.i(TAG, "内容:" + data.toString());
					 * }
					 */
				}
			}
		}
		return datas;
	}
	
	
	/**
	 * 友盟统计
	 */
	public void Stattistics(int position){
		Map<String, String> click = new HashMap<String, String>();
		click.put("plate",TITLE[position]);
		MobclickAgent.onEvent(this, "B00111", click);
	}
}
